<?php

namespace Admin\Controller;

use Think\Controller;
use Think\Page;

class ServerController extends Controller {

    public function __construct() {
        parent::__construct();
        session("[start]");        
        if (!isset($_SESSION['admininfo']) || $_SESSION['admininfo']['id'] < 1) {
            $this->redirect('Login/index');
        }
    }

    public function index() {
        //游戏ID
        $id = intval($_GET['id']);
        $serverModel = new \Admin\Model\ServerModel;
        $count = count($serverModel->where("game_id='" . $id . "'")->select());
        $Page = new Page($count, 10); // 实例化分页类 传入总记录数
        $show = $Page->show(); // 分页显示输出
        $list = $serverModel->where("game_id='" . $id . "'")->order('area_num desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $this->assign('list', $list); // 赋值数据集
        $this->assign('page', $show); // 赋值分页输出
        $this->assign('gid', $id);
        $this->display();
    }

    public function post() {
        $area_num = intval($_REQUEST['sid']);
        $game_id = intval($_REQUEST['gid']);
        //根据GID获得游戏名
        $gameModel = new \Admin\Model\GameModel;
        $game = $gameModel->where("id=%d", $game_id)->select();
        $gamename = $game[0]['gamename'];
        $serverModel = new \Admin\Model\ServerModel;
        if ($area_num && $game_id) {
            if (IS_POST) {
                //var_dump($_POST);exit;
                $data['area_num'] = I('post.area_num');
                $data['areaname'] = I('post.areaname');
                $data['open_time'] = strtotime(I('post.open_time'));
                $data['fee_time'] = strtotime(I('post.fee_time'));
                $data['maintenance_start'] = strtotime(I('post.maintenance_start'));
                $data['maintenance_end'] = strtotime(I('post.maintenance_end'));
                $data['state']=  intval($_POST['state']);
                $data['ip'] = I('post.ip');
                $data['domain'] = I('post.domain');
                $data['server_key'] = I('post.server_key');
                $data['ishf']=  intval($_POST['ishf']);
                $data['hf_gid']=  intval($_POST['hf_gid']);
                $data['hf_sid']=  intval($_POST['hf_sid']);
                if ($serverModel->where("game_id=%d and area_num=%d", array($game_id, $area_num))->save($data)) {
                    $this->success("服务器修改成功", "?s=/server/index/id/" . $game_id);
                } else {
                    $this->error("服务器修改失败");
                }
            }
            //修改            
            $server = $serverModel->where("game_id=%d and area_num=%d",array($game_id,$area_num))->select();
            //var_dump($server);
            $this->assign('sid',$area_num);
            $this->assign('gid',$game_id);
            $this->assign('gamename',$gamename);
            $this->assign('server', $server);
            $this->display();
        } else {
            //添加
            if(IS_POST){
                //var_dump($_POST);
                $data['area_num'] = I('post.area_num');
                $data['areaname'] = I('post.areaname');
                $data['open_time'] = strtotime(I('post.open_time'));
                $data['fee_time'] = strtotime(I('post.fee_time'));
                $data['maintenance_start'] = strtotime(I('post.maintenance_start'));
                $data['maintenance_end'] = strtotime(I('post.maintenance_end'));
                $data['ip'] = I('post.ip');
                $data['state']=  intval($_POST['state']);
                $data['domain'] = I('post.domain');
                $data['server_key'] = I('post.server_key');
                $data['game_id']=$game_id;
                $data['ishf']=  intval($_POST['ishf']);
                $data['hf_gid']=  intval($_POST['hf_gid']);
                $data['hf_sid']=  intval($_POST['hf_sid']);
                if($serverModel->data($data)->add()){
                    $this->success("服务器添加成功", "?s=/server/index/id/" . $game_id);
                }else{
                    $this->error("服务器添加失败");
                }
            }
            $this->assign('gid',$game_id);
            $this->assign('gamename', $gamename);
            $this->display();
        }
    }

}
